var urls = "http://localhost:8888/yihuan/";
var jurID = 3;

var imgObjTrue_0 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_0 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户昵称
var flas0 = false; //标识符
$("#userNickname").blur(function () {
    //取出用户昵称
    var nickname = $("#userNickname").val();
    //	用户昵称4-16位
    var usernameRegex = /^([a-zA-Z0-9_\u4e00-\u9fa5]{4,16})$/;
    //alert(varreg.test(name))
    if (nickname == null || nickname == "") {
        $("#recycle_0").empty();
        Swal.fire("提示", "用户昵称输入有误", "warning");
        $("#userNickname").val("");
        flas0 = false;
    } else if (!usernameRegex.test(nickname)) {
        $("#recycle_0").empty();
        Swal.fire("提示", "用户昵称输入有误", "warning");
        $("#userNickname").val("");
        flas0 = false;
    } else {
        $("#recycle_0").empty();
        flas0 = true;
        return;
    }
});
var imgObjTrue_1 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_1 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//账号验证
var flas = false; //标识符
$("#userLoginName").blur(function () {
    //取出账号值
    var name = $("#userLoginName").val();
    //		账号必须为字母+数字的组合，不能出现特殊字符
    var varreg = /^(?=.*[a-zA-Z]+)(?=.*[0-9]+)[a-zA-Z0-9]+$/;
    //alert(varreg.test(name))
    if (name == null || name == "") {
        $("#recycle_1").empty();
        Swal.fire("提示", "用户账号输入有误", "warning");
        $("#userLoginName").val("");
        flas = false;
    } else if (!varreg.test(name)) {
        $("#recycle_1").empty();
        Swal.fire("提示", "用户账号输入有误", "warning");
        $("#userLoginName").val("");
        flas = false;
    } else {
        $("#recycle_1").empty();
        findByLoginName();
        flas = true;
        return;
    }
});
var imgObjTrue_2 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_2 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//密码验证
var flas1 = false;
$("#userPassword").blur(function () {
    var pass = $("#userPassword").val(); //
    //密码规则为字母+数字的组合，不得出现特殊字符
    var patrm1 = /^[a-z0-9]{6,10}$/;
    if (pass == null || pass == "") {
        $("#recycle_2").empty();
        Swal.fire("提示", "用户密码输入有误", "warning");
        $("#userPassword").val("");
        flas1 = false;
    } else if (!patrm1.test(pass)) {
        $("#recycle_2").empty();
        Swal.fire("提示", "用户密码输入有误", "warning");
        $("#userPassword").val("");
        flas1 = false;
    } else {
        $("#recycle_2").empty();
        flas1 = true;
        return;
    }
});
var imgObjTrue_3 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_3 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//重复密码验证
var flas2 = false;
$("#userPassword_1").blur(function () {
    var pass2 = $("#userPassword_1").val();
    var pass1 = $("#userPassword").val();
    if (pass2 == null || pass2 == "") {
        $("#recycle_3").empty();
        Swal.fire("提示", "确认密码输入有误", "warning");
        $("#userPassword_1").val("");
        flas2 = false;
    } else if (pass2 != pass1) {
        $("#recycle_3").empty();
        Swal.fire("提示", "确认密码输入有误", "warning");
        $("#userPassword_1").val("");
        flas2 = false;
    } else {
        $("#recycle_3").empty();
        flas2 = true;
        return;
    }

});
var imgObjTrue_4 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_4 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户姓名验证
var flas3 = false;
$("#userName").blur(function () {
    var nc = $("#userName").val(); //
    var pattern = /^[\u4E00-\u9FA5]{2,6}$/;
    if (nc == null || nc == "") {
        $("#recycle_4").empty();
        Swal.fire("提示", "用户姓名输入有误", "warning");
        $("#userName").val("");
        flas3 = false;
    } else if (!pattern.test(nc)) {
        $("#recycle_4").empty();
        Swal.fire("提示", "用户姓名输入有误", "warning");
        $("#userName").val("");
        flas3 = false;
    } else {
        $("#recycle_4").empty();
        flas3 = true;
        return;
    }


});
var imgObjTrue_6 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_6 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//用户身份证号
var flas5 = false;
$("#userIDNumber").blur(function () {
    var sfz = $("#userIDNumber").val(); //
    //判断是身份证正则表达式
    var aa = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

    if (sfz == null || sfz == "") {
        $("#recycle_6").empty();
        Swal.fire("提示", "身份证号输入有误", "warning");
        $("#userIDNumber").val("");
        flas5 = false;

    } else if (!aa.test(sfz) || sfz.length != 18) {
        $("#recycle_6").empty();
        Swal.fire("提示", "身份证号输入有误", "warning");
        $("#userIDNumber").val("");
        flas5 = false;
    } else {
        $("#recycle_6").empty();
        flas5 = true;
        return;
    }
});
var imgObjTrue_7 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_7 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//邮箱验证
var flas6 = false;
$("#userEmail").blur(function () {
    var emil = $("#userEmail").val();
    //邮箱的正则表单达式
    var aa = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    if (emil == null || emil == "") {
        $("#recycle_7").empty();
        Swal.fire("提示", "电子邮箱输入有误", "warning");
        $("#userEmail").val("");
        flas6 = false;
    } else if (!aa.test(emil)) {
        $("#recycle_7").empty();
        Swal.fire("提示", "电子邮箱输入有误", "warning");
        $("#userEmail").val("");
        flas6 = false;
    } else {
        $("#recycle_7").empty();
        flas6 = true;
        return;
    }

});
var imgObjTrue_8 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_8 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//手机号验证
var flas7 = false;
$("#userPhone").blur(function () {
    var tell1 = $("#userPhone").val(); //
    //以1开头的正则表达式
    var aa = /^1\d{10}$/

    if (tell1 == null || tell1 == "") {
        $("#recycle_8").empty();
        Swal.fire("提示", "手机号码输入有误", "warning");
        $("#userPhone").val("");
        flas7 = false;
    } else if (!aa.test(tell1) || tell1.length != 11) {
        $("#recycle_8").empty();
        Swal.fire("提示", "手机号码输入有误", "warning");
        $("#userPhone").val("");
        flas7 = false;
    } else {
        $("#recycle_8").empty();
        flas7 = true;
        return;
    }
});
var imgObjTrue_9 = $("<img src='login/img/true.jpg' height=\"22\" width=\"23\">");
var imgObjFalse_9 = $("<img src='login/img/false.jpg' height=\"22\" width=\"23\">");
//家庭地址
var flas8 = false;
$("#userAddress").blur(function () {
    var sellerAddress = $("#userAddress").val(); //
    if (sellerAddress == null || sellerAddress == "") {
        $("#recycle_9").empty();
        Swal.fire("提示", "家庭地址输入有误", "warning");
        $("#userAddress").val("");
        flas8 = false;
    } else {
        $("#recycle_9").empty();
        flas8 = true;
        return;
    }
});


// 登录名称查询
function findByLoginName() {
    var userLoginName = $("#userLoginName").val();  //用户账号
    var data = {
        "userLoginName": userLoginName
    };
    $.ajax({
        url: urls + "findUserByUserLoginNameData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        async: false,
        success: function (data) {
            if (data.code == 200) {

            } else {
                Swal.fire("提示", "请重新输入账号", "error");
            }

        }
    });
}

//新增用户信息
function add_Data() {
    //个人地址
    var userAddress = $("#userAddress").val();
    var userNickname = $("#userNickname").val();
    var userLoginName = $("#userLoginName").val();
    var userPassword = $("#userPassword").val();
    var userName = $("#userName").val();
    var userSex = $("input[type='radio']:checked").val();
    var userIDNumber = $("#userIDNumber").val();
    var userPhone = $("#userPhone").val();
    var userEmail = $("#userEmail").val();
    var data = {
        "userAddress": userAddress,
        "userNickname": userNickname,
        "userLoginName": userLoginName,
        "userPassword": userPassword,
        "userName": userName,
        "userSex": userSex,
        "userIDNumber": userIDNumber,
        "userEmail": userEmail,
        "jurID": jurID,
        "userPhone": userPhone
    };
    swal.fire({
        type: 'warning', // 弹框类型
        title: '确认', //标题
        text: "确认是否注册用户", //显示内容
        confirmButtonColor: '#3085d6',// 确定按钮的 颜色
        confirmButtonText: '确定',// 确定按钮的 文字
        showCancelButton: true, // 是否显示取消按钮
        cancelButtonColor: '#d33', // 取消按钮的 颜色
        cancelButtonText: "取消", // 取消按钮的 文字
        focusCancel: true, // 是否聚焦 取消按钮
        reverseButtons: true  // 是否 反转 两个按钮的位置 默认是  左边 确定  右边 取消
    }).then(function (isConfirm) {
        if (isConfirm.value) {
            if (flas0 == true && flas1 == true && flas2 == true && flas3 == true && flas5 == true && flas6 == true && flas7 == true && flas8 == true && flas == true) {
                $.ajax({
                    url: urls + "insertUserByUserData",
                    type: "POST",
                    dataType: "json",
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify(data),
                    async: false,
                    success: function (data) {
                        if (data.code == 200) {
                            window.location.href = urls + "web-login.html";
                        } else {
                            Swal.fire("提示", "回收员注册失败！", "error");
                        }
                    }
                });
            } else {
                Swal.fire("提示", "输入数据有误", "error");
            }
        } else {
            Swal.fire("提示", "已取消提交", "error");
        }
    })
}

/*加载数据*/
$(function () {
    $("#add_Data").click(function () {
        add_Data();
    });
});